package com.ifinc.common.db;

import com.ifinc.common.web.frame.define.Dao;
import com.ifinc.common.web.frame.define.Persistence;
import org.apache.log4j.Logger;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: Administrator
 * Date: 2010-8-10
 * Time: 9:30:23
 */
public class BaseDaoIbatis extends SqlMapClientDaoSupport implements Dao {

    private static final String str_start = "start";

    private static final String str_pageSize = "pageSize";

    protected Logger log = Logger.getLogger(this.getClass());

    public List list(String sqlName, Map<String, String> param) {
        return getSqlMapClientTemplate().queryForList(sqlName, param);
    }

    public Pager listPage(String sqlName, Map<String, String> param, Pager pager) {
        param.put(str_start, String.valueOf(pager.getFirst() - 1));
        param.put(str_pageSize, String.valueOf(pager.getPageSize()));
        List result = getSqlMapClientTemplate().queryForList(sqlName, param);
        pager.setResult(result);
        return pager;
    }

    public int save(String sqlName, Map<String, String> param) {
        return getSqlMapClientTemplate().update(sqlName, param);
    }

    public int update(String sqlName, Map<String, String> param) {
        return getSqlMapClientTemplate().update(sqlName, param);
    }

    public int delete(String sqlName, Map<String, String> param) {
        return getSqlMapClientTemplate().update(sqlName, param);
    }

    @Override
    public Map load(String sql, Map<String, String> map) {
        List result = list(sql,map);
        if(result.size()<1){
            return null;
        }else if(result.size()>1){
            logger.warn("list' size >1 ,list size : "+result.size());
        }
        return (Map)result.get(0);
    }
}